package com.wkbb.basic.controller.open;

/**
 * @author Alger Yang
 * @date 2019/9/24 002419:27
 */

import com.alibaba.fastjson.JSON;
import com.wkbb.basic.service.JvtBoxService;
import com.wkbb.basic.utils.FileUtil;
import com.wkbb.basic.utils.jvt.JvtBoxBean;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URL;

/**
 * 巨龙盒子接口
 *
 * @author Alger Yang
 * @date 2019/9/24 002410:34
 */
@RestController
@Api(description = "巨龙盒子接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class JvtBoxController {

    @Autowired
    private JvtBoxService jvtBoxService;

    @ApiOperation(value = "获取巨龙盒子信息")
    @RequestMapping(value = "getJvtBoxInfo", method = RequestMethod.GET)
    public JvtBoxBean getJvtBoxInfo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid) {
        log.info("***********JvtBoxController.getJvtBoxInfo******apiVersion:{},UUID:{}", apiVersion, uuid);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.getJvtBoxInfo(uuid);
        } catch (Exception e) {
            log.error("***********JvtBoxController.getJvtBoxInfo******", e);
            return null;
        }
    }

    @ApiOperation(value = "NTP时间同步")
    @RequestMapping(value = "syncJvtTime", method = RequestMethod.GET)
    public JvtBoxBean syncJvtTime(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid) {
        log.info("***********JvtBoxController.syncJvtTime******apiVersion:{},UUID:{}", apiVersion, uuid);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.syncJvtTime(uuid);
        } catch (Exception e) {
            log.error("***********JvtBoxController.syncJvtTime******", e);
            return null;
        }
    }

    @ApiOperation(value = "上传参数设置")
    @RequestMapping(value = "setJvtParams", method = RequestMethod.POST)
    public JvtBoxBean setJvtParams(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid) {
        log.info("***********JvtBoxController.setJvtParams******apiVersion:{},UUID:{}", apiVersion, uuid);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.setJvtParams(uuid);
        } catch (Exception e) {
            log.error("***********JvtBoxController.setJvtParams******", e);
            return null;
        }
    }

    @ApiOperation(value = "上传参数获取")
    @RequestMapping(value = "getJvtParams", method = RequestMethod.GET)
    public JvtBoxBean getJvtParams(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid) {
        log.info("***********JvtBoxController.getJvtParams******apiVersion:{},UUID:{}", apiVersion, uuid);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.getJvtParams(uuid);
        } catch (Exception e) {
            log.error("***********JvtBoxController.getJvtParams******", e);
            return null;
        }
    }

    @ApiOperation(value = "获取人员名单")
    @RequestMapping(value = "getJvtPersonList", method = RequestMethod.GET)
    public JvtBoxBean getJvtPersonList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType) {
        log.info("***********JvtBoxController.getJvtPersonList******apiVersion:{},UUID:{},personType:{}", apiVersion, uuid, personType);

        try {
            if (StringUtil.isNullOrBlank(uuid) || personType == null) {
                return null;
            }

            return jvtBoxService.getJvtPersonList(uuid, personType);
        } catch (Exception e) {
            log.error("***********JvtBoxController.getJvtPersonList******", e);
            return null;
        }
    }

    @ApiOperation(value = "获取人员信息")
    @RequestMapping(value = "getJvtPerson", method = RequestMethod.GET)
    public JvtBoxBean getJvtPerson(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType,
            @ApiParam(value = "人员ID（userCode）", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "是否返回人员相片", required = false) @RequestParam(name = "getPhoto", required = false) Boolean getPhoto) {
        log.info("***********JvtBoxController.getJvtPerson******apiVersion:{},UUID:{},personType{},personId:{},getPhoto{}",
                apiVersion, uuid, personType, personId, getPhoto);

        try {
            if (StringUtil.isNullOrBlank(uuid)
                    || personType == null
                    || StringUtil.isNullOrBlank(personId)) {
                return null;
            }

            return jvtBoxService.getJvtPerson(uuid, personType, personId, getPhoto == null ? false : getPhoto);
        } catch (Exception e) {
            log.error("***********JvtBoxController.getJvtPerson******", e);
            return null;
        }
    }

    @ApiOperation(value = "添加人员信息")
    @RequestMapping(value = "addJvtPerson", method = RequestMethod.GET)
    public JvtBoxBean addJvtPerson(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType,
            @ApiParam(value = "人员ID（userCode）", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "人员相片Url", required = true) @RequestParam(name = "faceUrl", required = true) String faceUrl) {
        log.info("***********JvtBoxController.addJvtPerson******apiVersion:{,UUID:{},personType{},personId:{},faceUrl{}",
                apiVersion, uuid, personType, personId, faceUrl);

        try {
            if (StringUtil.isNullOrBlank(uuid)
                    || personType == null
                    || StringUtil.isNullOrBlank(personId)
                    || StringUtil.isNullOrBlank(faceUrl)) {
                return null;
            }

            return jvtBoxService.addJvtPerson(uuid, personType, personId, FileUtil.encodeImageToJpgBase64(new URL(faceUrl)).replace("\r", "").replace("\n", ""));
        } catch (Exception e) {
            log.error("***********JvtBoxController.addJvtPerson******", e);
            return null;
        }
    }

    @ApiOperation(value = "删除人员名单")
    @RequestMapping(value = "deleteJvtPersonList", method = RequestMethod.GET)
    public JvtBoxBean deleteJvtPersonList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType) {
        log.info("***********JvtBoxController.deleteJvtPersonList******apiVersion:{},UUID:{},personType:{}", apiVersion, uuid, personType);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.deleteJvtPersonList(uuid, personType);
        } catch (Exception e) {
            log.error("***********JvtBoxController.deleteJvtPersonList******", e);
            return null;
        }
    }

    @ApiOperation(value = "删除人员信息")
    @RequestMapping(value = "deleteJvtPerson", method = RequestMethod.GET)
    public JvtBoxBean deleteJvtPerson(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType,
            @ApiParam(value = "人员ID（userCode）", required = true) @RequestParam(name = "personId", required = true) String personId) {
        log.info("***********JvtBoxController.deleteJvtPerson******apiVersion:{},UUID:{},personType:{},personId:{}", apiVersion, uuid, personType, personId);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return null;
            }

            return jvtBoxService.deleteJvtPerson(uuid, personType, personId);
        } catch (Exception e) {
            log.error("***********JvtBoxController.deleteJvtPerson******", e);
            return null;
        }
    }

    @ApiOperation(value = "新增添加人员信息任务")
    @RequestMapping(value = "pushAddJvtPerson", method = RequestMethod.POST)
    public Object pushAddJvtPerson(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType,
            @ApiParam(value = "人员ID（userCode）", required = true) @RequestParam(name = "personId", required = true) String personId,
            @ApiParam(value = "人员相片Url", required = true) @RequestParam(name = "faceUrl", required = true) String faceUrl) {
        log.info("***********JvtBoxController.pushAddJvtPerson******apiVersion:{,UUID:{},personType{},personId:{},faceUrl{}",
                apiVersion, uuid, personType, personId, faceUrl);

        try {
            if (StringUtil.isNullOrBlank(uuid)
                    || personType == null
                    || StringUtil.isNullOrBlank(personId)
                    || StringUtil.isNullOrBlank(faceUrl)) {
                return "params miss";
            }

            jvtBoxService.pushAddJvtPersonTask(uuid, personType, personId, faceUrl);
            return "success";
        } catch (Exception e) {
            log.error("***********JvtBoxController.pushAddJvtPerson******", e);
            return "failure";
        }
    }

    @ApiOperation(value = "新增删除人员名单任务")
    @RequestMapping(value = "pushDeleteJvtPersonList", method = RequestMethod.POST)
    public Object pushDeleteJvtPersonList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType) {
        log.info("***********JvtBoxController.pushDeleteJvtPersonList******apiVersion:{},UUID:{},personType:{}", apiVersion, uuid, personType);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return "params miss";
            }

            jvtBoxService.pushDeleteJvtPersonListTask(uuid, personType);
            return "success";
        } catch (Exception e) {
            log.error("***********JvtBoxController.pushDeleteJvtPersonList******", e);
            return "failure";
        }
    }

    @ApiOperation(value = "新增删除人员信息任务")
    @RequestMapping(value = "pushDeleteJvtPerson", method = RequestMethod.POST)
    public Object pushDeleteJvtPerson(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备UUID", required = true, defaultValue = "umetihnf3t5x") @RequestParam(name = "UUID", required = true) String uuid,
            @ApiParam(value = "名单类型：1：黑名单 2：白名单 3：VIP名单", required = true) @RequestParam(name = "personType", required = true) Integer personType,
            @ApiParam(value = "人员ID（userCode）", required = true) @RequestParam(name = "personId", required = true) String personId) {
        log.info("***********JvtBoxController.pushDeleteJvtPerson******apiVersion:{},UUID:{},personType:{},personId:{}", apiVersion, uuid, personType, personId);

        try {
            if (StringUtil.isNullOrBlank(uuid)) {
                return "params miss";
            }

            jvtBoxService.pushDeleteJvtPersonTask(uuid, personType, personId);
            return "success";
        } catch (Exception e) {
            log.error("***********JvtBoxController.deleteJvtPerson******", e);
            return "failure";
        }
    }

    @ApiOperation(value = "注册回执")
    @RequestMapping(value = "registerJvtBox", method = RequestMethod.POST)
    public JvtBoxBean registerJvtBox(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "注册信息", required = true) @RequestBody Object object) {
        log.info("***********JvtBoxController.registerJvtBox******apiVersion:{},jvtBoxBean:{}", apiVersion, object);

        try {
            return jvtBoxService.registerJvtBox(JSON.parseObject(JSON.toJSONString(object), JvtBoxBean.class));
        } catch (Exception e) {
            log.error("***********JvtBoxController.registerJvtBox******", e);
            return null;
        }
    }

    @ApiOperation(value = "心跳回执")
    @RequestMapping(value = "heartbeatJvtBox", method = RequestMethod.POST)
    public JvtBoxBean heartbeatJvtBox(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "心跳信息", required = true) @RequestBody Object object) {
        log.info("***********JvtBoxController.heartbeatJvtBox******apiVersion:{},jvtBoxBean:{}", apiVersion, object);

        try {
            return jvtBoxService.heartbeatJvtBox(JSON.parseObject(JSON.toJSONString(object), JvtBoxBean.class));
        } catch (Exception e) {
            log.error("***********JvtBoxController.heartbeatJvtBox******", e);
            return null;
        }
    }

}
